package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.biz.TransOutboundInvoker;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.factory.OrderProcessPipelineFactory;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dal.persistence.StockMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {


	@Autowired
    OrderMapper orderMapper;

	@Autowired
    OrderItemMapper orderItemMapper;

	@Autowired
    OrderShippingMapper orderShippingMapper;

	@Autowired
    OrderProcessPipelineFactory orderProcessPipelineFactory;

	@Autowired
    StockMapper stockMapper;

	@Autowired
	OrderConverter orderConverter;

	@Override
	public OrderListResponse orderList(OrderListRequest request) {
		OrderListResponse response = new OrderListResponse();
		try {
			request.requestCheck();
			PageHelper.startPage(request.getPage(), request.getSize(), request.getSort());
			List<OrderDetailInfo> detailInfoList = orderMapper.orderList(request.getUserId());
			PageInfo pageInfo = new PageInfo(detailInfoList);
			// 返回
			// response.setDetailInfoList(detailInfoList);
			response.setData(detailInfoList);
			response.setTotal(pageInfo.getTotal());
			response.setCode(ShoppingRetCode.SUCCESS.getCode());
			//response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

		} catch (Exception e) {
			log.error("OrderQueryServiceImpl.orderList occur Exception :" + e);
			ExceptionProcessorUtils.wrapperHandlerException(response, e);
		}

		return response;
	}

	@Override
	public OrderDetailNewResponse orderDetail(OrderDetailRequest request) {
		OrderDetailNewResponse response = new OrderDetailNewResponse();

		try {
			request.requestCheck();
//            Example example = new Example(OrderItem.class);
//            example.createCriteria().andEqualTo("orderId",request.getOrderId());
			String orderId = request.getOrderId();
			List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
			List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
			System.out.println(orderItems);
//            int count = orderItemMapper.selectOrderDetail1(orderId);
//            double orderTotal = 0;
//            for (OrderItem orderItemDto : orderItems) {
//                for (int i = 0; i < count; i++) {
//                    double s  = orderItemDto.getNum() * orderItemDto.getPrice();
//                    orderItemDto.setTotalFee(s);
//                    orderTotal += orderItemDto.getTotalFee();
//                }
//                response.setOrderTotal(orderTotal);
//            }
//            String userInfo = (String) request.getHeader("user_info");
//            JSONObject object = JSON.parseObject(userInfo);
//            Long uid = Long.parseLong(object.get("uid").toString());
			OrderDetailNewResponse orderDetailNewResponse = orderMapper.selectOrderDetail(orderId);
			response.setOrderStatus(orderDetailNewResponse.getOrderStatus());
			response.setOrderTotal(orderDetailNewResponse.getOrderTotal());
			response.setStreetName(orderDetailNewResponse.getStreetName());
			response.setTel(orderDetailNewResponse.getTel());
			response.setUserId(orderDetailNewResponse.getUserId());
			response.setUserName(orderDetailNewResponse.getUserName());
			response.setGoodsList(orderItemDtos);
			System.out.println(response);
			response.setCode(OrderRetCode.SUCCESS.getCode());
			response.setMsg(OrderRetCode.SUCCESS.getMessage());
			return response;
		}catch (Exception e){
			e.printStackTrace();
			ExceptionProcessorUtils.wrapperHandlerException(response, e);
		}
		return response;
	}
}
